﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DuelQuizzServer
{
    public partial class Question
    {
        private static Entities context;
        private static DbSet<Question> entities
        {
            get
            {
                if (Question.context == null)
                {
                    Question.context = new Entities();
                    Question.context.Configuration.ProxyCreationEnabled = false;
                    Question.context.Configuration.LazyLoadingEnabled = true;
                }
                return context.Question;
            }
        }

        internal static bool add(Question question)
        {
            try
            {
                Question.entities.Add(question);
                Question.context.SaveChanges();
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        internal static List<Question> getQuestionsFromCategory(int category_id, int quantity)
        {
            List<Question> list = Question.entities.Where(c => (c.category_id == category_id)).ToList();
            if (list == null) return new List<Question>();

            list =  Utils.shuffle(list).Take(quantity).ToList();
            // load reponse
            foreach(Question question in list){
                question.answers = Answer.getAnswerFromQuestion(question);
            }

            return list;
        }

    }
} 